<?php
/**
 * 合成类
 * User: Yan
 * Date: 2018/7/23
 * Time: 下午 11:38
 */

namespace YanQR;


class QRCompose
{
    private $qrcode;
    private $text;
    private $bottomPath;

    public function setBottomPath($path)
    {
        if (empty($path)) return false;

        $this->bottomPath = $path;
    }

    public function setQrInfo($array)
    {
        if (empty($array)) return false;

        $this->qrcode[] = $array;
    }

    public function setText($array)
    {
        if (empty($array)) return false;

        $this->text[] = $array;
    }

    public function compose($print = true)
    {
        # 创建图片对象
        $image_bottom = imagecreatefrompng($this->bottomPath);

        if (!empty($this->qrcode)) {
            # 合成图片
            foreach ($this->qrcode as $item) {
                $image_tem = imagecreatefrompng($item['path']);
                imagecopymerge($image_bottom, $image_tem, $item['position_x'], $item['position_y'], $item['show_x'], $item['show_y'], imagesx($image_tem), imagesy($image_tem), 100);
            }
        }

        $font = APP_PATH . '/public/font/STHeiti.ttc';//字体


        if (!empty($this->text)) {
            # 合成图片
            foreach ($this->text as $item) {
                $color = imagecolorallocate($image_bottom, $item['color'][0], $item['color'][1], $item['color'][2]);//字体颜色
                imagefttext($image_bottom, $item['size'], 0, $item['position_x'], $item['position_y'], $color, $font, $item['text']);
            }
        }

        if ($print) {
            header("Content-type: image/png");
            imagepng($image_bottom);
        }

        return $image_bottom;
    }


}